package com.amos.cmp_android.ui;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amos.cmp_android.R;
import com.amos.cmp_android.model.AcademyModel;
import com.amos.cmp_android.model.DetachmentModel;
import com.amos.cmp_android.model.FunctionDepartmentModel;
import com.amos.cmp_android.model.SubDepartmentModel;
import com.amos.cmp_android.net.STError;
import com.amos.cmp_android.net.STNetTask;
import com.amos.cmp_android.presenter.GetFunctionInfoPresenter;

import java.util.ArrayList;

/**
 * Created by tlh on 2016/4/13.
 */
public class YellowPageActivity extends BaseActivity implements
        STNetTask.BeforeQueryHook, STNetTask.AfterQueryHook,GetFunctionInfoPresenter.GetFunctionInfoListener,
        SwipeRefreshLayout.OnRefreshListener{
    private GetFunctionInfoPresenter presenter;
    private BaseRecyclerAdapter<Object> adapter;
    private ArrayList<Object> mList;

    @Override
    protected String getToolbarTitle() {
        return "黄页信息浏览";
    }

    @Override
    protected void initData() {
        super.initData();
        mList = new ArrayList<>();
        swipeRefresh.setColorSchemeResources(R.color.blue, R.color.gray, R.color.green, R.color.red);//设置跑动的颜色值
        swipeRefresh.setOnRefreshListener(this);
        adapter=new BaseRecyclerAdapter<Object>(this,mList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycler_item_notebook;
            }
            @Override
            public void bindData(RecyclerViewHolder holder, int position, Object item) {
                LinearLayout layout= (LinearLayout) holder.getView(R.id.recycler_item_ll_notebook);
                // TODO: 2016/4/13 为Item的控件进行数据绑定,Item UI待定
                if (item instanceof FunctionDepartmentModel){//职能部门
                    FunctionDepartmentModel model= (FunctionDepartmentModel) item;
                    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    TextView tv_name = new TextView(YellowPageActivity.this);
                    tv_name.setText(model.getFname());
                    tv_name.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_name,params);
                    TextView tv_tel = new TextView(YellowPageActivity.this);
                    tv_tel.setText(model.getFtel());
                    tv_tel.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_tel,params);
                    TextView tv_type = new TextView(YellowPageActivity.this);
                    tv_type.setText(model.getFtype());
                    tv_type.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_type,params);
                }else if(item instanceof SubDepartmentModel){//直属部门
                    SubDepartmentModel model= (SubDepartmentModel) item;
                    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    TextView tv_name = new TextView(YellowPageActivity.this);
                    tv_name.setText(model.getSname());
                    tv_name.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_name,params);
                    TextView tv_tel = new TextView(YellowPageActivity.this);
                    tv_tel.setText(model.getStel());
                    tv_tel.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_tel,params);
                }else if (item instanceof DetachmentModel){//附属部门
                    DetachmentModel model= (DetachmentModel) item;
                    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    TextView tv_name = new TextView(YellowPageActivity.this);
                    tv_name.setText(model.getDname());
                    tv_name.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_name,params);
                    TextView tv_tel = new TextView(YellowPageActivity.this);
                    tv_tel.setText(model.getDtel());
                    tv_tel.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_tel,params);
                }else if (item instanceof AcademyModel){
                    AcademyModel model= (AcademyModel) item;
                    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    TextView tv_name = new TextView(YellowPageActivity.this);
                    tv_name.setText(model.getAname());
                    tv_name.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_name,params);
                    TextView tv_addr = new TextView(YellowPageActivity.this);
                    tv_addr.setText(model.getAaddress());
                    tv_addr.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_addr,params);
                    TextView tv_tel = new TextView(YellowPageActivity.this);
                    tv_tel.setText(model.getAtel());
                    tv_tel.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_tel,params);
                    TextView tv_web = new TextView(YellowPageActivity.this);
                    tv_web.setText(model.getAwebsite());
                    tv_web.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    layout.addView(tv_web,params);

                }
            }
        };
        recyclerView.setAdapter(adapter);
        presenter = new GetFunctionInfoPresenter(this);
        presenter.setBeforeQueryHook(this);
        presenter.setAfterQueryHook(this);
        presenter.setListener(this, mList);
        presenter.getInfo(1,50);
    }

    @Override
    public void afterQueryHook() {
        if (swipeRefresh != null && swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(false);
    }

    @Override
    public void beforeQueryHook() {
        if (swipeRefresh != null && !swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(true);
    }

    @Override
    public void onSuccess() {
        if (adapter != null)
            adapter.notifyDataSetChanged();
        if (swipeRefresh != null && swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(false);
        Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed(STError error) {
        Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        //重新获取数据
        presenter.getInfo(1,50);
    }

}
